/**
 * Select Component Logic
 * handle codemap data [ codemap.js in old system ]
 * To the Dev: must implement [handleRemoteCall] method
 * @author: Merjiezo
 * @since: 2018-08-14
 * @version 1.0.0
 */
const noopData = (data) => { return data }
function linkageToSetOtherData (linkObj, value, linkList, extObj) {
  if (value) {
    linkList.forEach(item => {
      let itemKeyObj = typeof item === 'string'
        ? { from: item, to: item } : item
      linkObj[itemKeyObj.to] = extObj[itemKeyObj.from] || ''
    })
  } else {
    linkList.forEach(item => {
      let resetItemKey = typeof item === 'string'
        ? item : item.to
      linkObj[resetItemKey] = ''
    })
  }
}

export default {

  props: {
    dropWidth: String,
    disabled: Boolean,
    form: Object,
    name: String,
    placeholder: {
      type: String,
      default: '请选择'
    },
    max: {
      type: Number,
      default: 10
    },
    /**
     * Both db and loc components have
     * @param {String} label [just a word]
     * @param {Array} filterArr filter parames to Render selectItem， render formatter `${filterArr[0]} [${filterArr[1]}]`
     * @param {Array} linkageList link set value [Can be null]
     * @param {String|Object} linkageList[num]
     * [example: body105: [ 'body10501', 'body10501Cn' ] | otherVal: { from: 'fromVal', to: 'setToVal' } ]
     * 
     * Only in loc | base
     * @param {String} codeKey code key [example: head002]
     * 
     * Only in db
     * @param {Object} dbSet Database API parames
     * @param {String} dbSet.api [Can be null] api.json key
     */
    setting: Object,
    beforeRender: {
      type: Function,
      default: noopData
    }
  },

  mounted () {
    // 初始化设置新的可赋值内容
    if (this.componentType === 'select'
      || !this.componentType) {
      this.$on('get-current-val', this.getCurrentVal)
    }
  },

  methods: {
    /**
     * 简单的赋值其他字段通过 linkageList 配置；有复杂操作的外部处理
     */
    handleChange(value, selectItemVm) {
      // [EP-UI improve] 统一输出
      let extObj
      if (this.componentType === 'search') {
        extObj = selectItemVm || {}
      } else {
        extObj = selectItemVm ? (selectItemVm.extObj || {}) : {}
      }
      let linkList = this.setting.linkageList || []
      linkageToSetOtherData(this.form, value, linkList, extObj)
      this.$emit('change', value, selectItemVm, extObj)
    },
    handleTabEvt () {
      let { nowSelect, selectors } = this.$refs.select
      // 有值则返回
      if (this.$options.name === 'remote-codemap-select'
        && this.form[this.name]) {
        return
      }
      // let selVueInstance = nowSelect !== -1 && selectors[nowSelect]
      //   ? selectors[nowSelect] : getSelectSetInstance(selectors)

      let selVueInstance = (nowSelect !== -1 && selectors[nowSelect])
        ? selectors[nowSelect] : ''

      // triggert click Evt in [ EP-UI ] SelectItem component
      if (selVueInstance) {
        selVueInstance.$el.click()
      }
    },
    setVal (res, filterArr, callback) {
      let result = []
      // res = this.beforeRender(res) // 以后个别项 disabled 什么的时候可以用到
      res.forEach(item => {
        let elementItem = { ...item }
        elementItem.value = item[filterArr[0]]
        if (filterArr.length > 1) {
          elementItem.label =  `${item[filterArr[0]]} [${item[filterArr[1]]}]`
        } else {
          elementItem.label =  `${item[filterArr[0]]}`
        }
        result.push(elementItem)
      })
      callback.done(result)
    }
  }
}
